package com.jack.design_patterns.creational.single;

/**
 * 懒汉式单例-双重检查
 *  优点：解决了线程同步问题的同时，效率比较高效
 */
public class DoubleCheckLazySingleton {

    // volatile防止指令重排
    private volatile static DoubleCheckLazySingleton INSTANCE;

    private DoubleCheckLazySingleton() {

    }

    public static DoubleCheckLazySingleton getInstance() {
        if (INSTANCE == null) {
            synchronized (DoubleCheckLazySingleton.class) {
                if (INSTANCE == null) {
                    // 这里存在指令重排序的问题
                    INSTANCE = new DoubleCheckLazySingleton();
                }
            }
        }
        return INSTANCE;
    }
}
